package com.pty.mainservice.controller;

import com.pty.mainservice.model.User;
import com.pty.mainservice.service.UserService;
import com.pty.mainservice.util.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by pty on 2017/12/22
 */
@Controller
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate<String, String> template;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    // inject the template as ListOperations
    @Resource(name = "redisTemplate")
    private ListOperations<String, String> listOps;

    @RequestMapping("/setStr/{key}/{value}")
    @ResponseBody
    public void setStrVal(@PathVariable String key, @PathVariable String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    @RequestMapping("/getStr/{key}")
    @ResponseBody
    public String getStrVal(@PathVariable String key) {
        log.info(key);
        return stringRedisTemplate.opsForValue().get(key);
    }

    @RequestMapping("/get/{str}")
    @ResponseBody
    public String getValue(@PathVariable String str) {
        return template.opsForList().rightPop(str);
    }

    @RequestMapping("/set/{key}/{str}")
    @ResponseBody
    public Long getValue(@PathVariable String key, @PathVariable String str) {
        return listOps.leftPush(key, str);
    }

    @RequestMapping("/info/{id}")
    @ResponseBody
    public String getInfo(@PathVariable Integer id) {
        User user = userService.selectByPrimaryKey(id);
        log.info(user.toString() + "1212");
        return user.getUsername() + " 《》《》 " + user.getPassword();
    }

    @RequestMapping(value = "getInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getInfo(@RequestBody Map<String, Object> param) throws Exception {
        User user = userService.selectByPrimaryKey(Integer.valueOf(param.get("id").toString()));
        return BeanUtils.transBean2Map(user);
    }
}
